package blingking.jfinal.orm.aop;


import blingking.jfinal.orm.tx.TxTypeEnum;
import blingking.jfinal.orm.annotation.JFinalORMTx;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author King
 * @description
 * @Date 2020/3/22
 */
@Aspect
@Component
public class JFinalORMTxAop {

    @Pointcut("@annotation(blingking.jfinal.orm.annotation.JFinalORMTx)")
    private void methodAnno() {
    }

    private TxTypeEnum txTypeEnum;

    @Around(value = "methodAnno()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        getTxType(pjp);
        Object result = txTypeEnum.execAOPTx(pjp);
        return result;
    }

    public void getTxType(ProceedingJoinPoint pjp) {
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        Method method = ms.getMethod();
        JFinalORMTx jFinalORMTx = method.getAnnotation(JFinalORMTx.class);
        txTypeEnum = jFinalORMTx.txType();

    }

}
